package org.jeecg.modules.equipment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.equipment.entity.Filterelement;
import org.jeecg.modules.equipment.entity.FilterelementReplace;
import org.jeecg.modules.equipment.service.FilterelementReplaceService;
import org.jeecg.modules.equipment.service.FilterelementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/9 10:38
 * @Description:
 */
@RestController
//swagger注解，指定controller名称
@Api(tags = "滤芯控制层")
//按模块区分请求路径
@Log4j2
@RequestMapping("/equipment/filterelement")
public class FilterelementController {

    @Autowired
    private FilterelementService filterelementService;
    @Autowired
    private FilterelementReplaceService filterelementReplaceService;

    /**
     * 滤芯分页查询接口
     *
     * @param filterelement 动态查询条件对象
     * @param pageNo        分页页码
     * @param pageSize      每页大小
     * @param request       请求对象
     * @return
     * @Author zhuzhuhong
     * @Date 2019/11/9
     */
    @AutoLog(value = "滤芯表-分页列表查询")
    //swagger注解，指定不同的处理器
    @ApiOperation(value = "滤芯表-分页列表查询", notes = "滤芯表-分页列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<Filterelement>> findFitlerelementByPage(
            Filterelement filterelement,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        Result<IPage<Filterelement>> pageResult = new Result<>();
        try {
            //封装查询条件
            /*Map<String, String[]> parameterMap = request.getParameterMap();
            QueryWrapper<Filterelement> filterelementQueryWrapper = QueryGenerator
                    .initQueryWrapper(filterelement, parameterMap);*/
            //模糊查询
            String filterelementName = request.getParameter("filterelementName");
            //将其封装到Wrapper对象中
            QueryWrapper<Filterelement> filterelementQueryWrapper = new QueryWrapper<Filterelement>()
                    .like(StringUtils.isNotBlank(filterelementName) ? true : false,
                            "filterelement_name", filterelementName)
                    .orderByDesc("create_time");
            //分页对象
            IPage<Filterelement> iPage = new Page<>(pageNo, pageSize);
            filterelementService.page(iPage, filterelementQueryWrapper);
            pageResult.setSuccess(true);
            //将数据封装到Result中，返回给前端
            pageResult.setResult(iPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            pageResult.setMessage("后台异常");
            pageResult.setSuccess(false);
        }
        return pageResult;
    }

    @PostMapping(value = "/add")
    public Result<Object> addFilterelement(@RequestBody Filterelement filterelement) {
        Result<Object> result = new Result<>();
        try {
            //执行新增操作
            boolean saveResult = filterelementService.save(filterelement);
            //新增操作失败
            if (!saveResult) {
                result = Result.error("操作失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result = result.error500("后台异常");
        }
        return result;
    }

    @DeleteMapping(value = "/del")
    public Result<Object> deleteFilterelementById(String id) {
        Result<Object> result = new Result<>();
        try {
            //判断当前id所对应的滤芯是否被其他商品类型关联
            //先查询滤芯更换记录表，如果 对应的滤芯不为空,则证明该滤芯正在被 使用
            QueryWrapper<FilterelementReplace> queryWrapper = new QueryWrapper<FilterelementReplace>()
                    .eq("filterelement_id", id);
            //获取到当前滤芯所对应的更换记录数量，如果大于0,证明该滤芯正在被 使用
            int count = filterelementReplaceService.count(queryWrapper);
            if (count > 0) {
                result = Result.error("该滤芯正在被使用，不能被删除");
            } else {
                //执行删除操作
                boolean removeResult = filterelementService.removeById(id);
                if (!removeResult) {
                    result = Result.error("删除失败");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("后台异常");
        }
        return result;
    }
}
